package com.study.living;

import android.Manifest;
import android.os.Bundle;
import android.widget.Button;

import com.study.living.camera.AutoFitTextureView;
import com.study.living.camera.CameraProxy;

public class MainActivity extends BaseActivity {
    private static final int REQUEST_CODE_CAMERA = 0x123;

    //相机权限
    private String[] cameraPermissions = new String[]{
            Manifest.permission.CAMERA
    };
    private AutoFitTextureView textureView;
    private Button btnStartRecorder;
    private CameraProxy mCamera;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textureView = findViewById(R.id.textureView);
        btnStartRecorder = findViewById(R.id.btnStartRecorder);
        mCamera = new CameraProxy(this, textureView);

        btnStartRecorder.setOnClickListener(v -> {
            startRecorder();
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (hasDangerousPermissions(cameraPermissions)){
            openCamera();
        } else {
            requestDangerousPermissions(cameraPermissions, REQUEST_CODE_CAMERA);
        }
    }

    @Override
    public boolean handlePermissionResult(boolean isGranted, int requestCode) {
        if (requestCode == REQUEST_CODE_CAMERA){
            openCamera();
            return true;
        }
        return super.handlePermissionResult(isGranted, requestCode);
    }

    private void openCamera(){
        runTaskOnVisibleToUser(() -> mCamera.start());
    }

    private void startRecorder() {

    }

}